package org.hhttp.response;

import java.io.OutputStream;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.hhttp.core.Constants;
import org.hhttp.core.Response;
import org.hhttp.objectPool.BoundedBlockingPool;

public class ResponsePool {
	
	private static Logger logger = Logger.getLogger(ResponsePool.class);
	
	private static BoundedBlockingPool<Response> pool;

	public static Response get(OutputStream outputStream){
		
		Response response = null;
		try {
			if(pool == null){
				pool = new BoundedBlockingPool<Response>(Constants.object_poll_num, 
						new ResponseValidator() , 
						new ResponseFactory(), 
						Executors.newCachedThreadPool());
			}
			response = pool.get();
			response.setOutputStream(outputStream);
		} catch (Exception e) {
			logger.error(e);
		}
		return response;
	}
	public static void release(Response t){
		if(pool != null){
			t.reset();
			pool.release(t);
		}
	}
}
